
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import Home from '../views/Home.vue'
import { getLocal } from '@/common/ts/util.ts'
const routes: Array<RouteRecordRaw> = [
  { //默认显示首页
    path:'/',
    component: Home,
    meta: {
      index:1
    }
  },
  { //首页路由
    path:'/home',
    name:'home',
    component: Home,
    meta: {
      index:1
    }
  },
  { //登录注册路由
    path:'/login',
    name:'login',
    component: () => import('../views/Login.vue'),
    meta: {
      index:1
    }
  },
  {// 搜索页路由
    path: '/productList',
    name: 'productList',
    component: () => import('../views/ProductList.vue'),
    meta: {
      index:2
    }
  },
  {// 购物车路由
    path: '/cart',
    name: 'cart',
    component: () => import('../views/Cart.vue'),
    meta: { index: 1 }
  },
  {// 生成订单路由
    path: '/create-order',
    name: 'create-order',
    component: () => import('../views/CreateOrder.vue'),
    meta: { index: 2 }
  },
  {// 我的路由
    path: '/user',
    name: 'user',
    component: () => import(/* webpackChunkName: "user" */ '../views/User.vue'),
    meta: { index: 1 }
  },
  {
    path: '/order',
    name: 'order',
    component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue'),
    meta: { index: 2 }
  },
  {
    path: '/category',
    name: 'category',
    component: ()=>import ('../views/Classification.vue')
  },
  // 商品详情路由 使用了占位符
  {
    path: '/product/:id',
    name: 'product',
    component: () => import('../views/ProductDetail.vue'),
    meta: {
      index: 2
    }
  },
  {
    path: '/product-list',
    name: 'product-list',
    component: () => import(/* webpackChunkName: "product-list" */ '../views/ClassificationDetailSearch.vue'),
    meta: {
      index: 2
    }
  },
  {

    path: "/setting",
    name: 'setting',
    component: () => import(/* webpackChunkName: "setting" */ '../views/Setting.vue'),
    meta: { index: 2 }
  },
  {
    path: "/address",
    name: 'address',
    component: () => import(/* webpackChunkName: "address" */ '../views/Address.vue'),
    meta: { index: 2 }
  },
  {
    path: '/order-detail',
    name: 'order-detail',
    component: () => import(/* webpackChunkName: "order-detail" */ '../views/OrderDetail.vue'),
    meta: { index: 2 }
  },
  {
    path: '/address-edit',
    name: 'address-edit',
    component: () => import(/* webpackChunkName: "order-detail" */ '../views/AdressEdit.vue'),
    meta: { index: 2 }
  },
]
const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to,from,next)=>{
  if(to.path == '/login') {
    next()
  } else if (!localStorage.getItem('token')) {
    if (to.path === '/home' || to.path == '/') {
        next()
    }
    else {
        next({path:'/login'})
    }
} else {
    next()
}
})

export default router
